package com.book.mapper;

import com.book.entity.Book;
import com.book.entity.Borrow;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * @Author zzw2000
 * @Date 2022年08月27日 7:38
 * @Description 描述
 */
@Mapper
public interface BookMapper {

    @Select("select * from book where status=0")
    List<Book> bookList();

    @Delete("delete from book where bid=#{id}")
    void delBook(int id);

    @Insert("insert into book (title, `desc`, price) values (#{title},#{desc},#{price})")
    void addBook(@Param("title") String title, @Param("desc") String desc, @Param("price") double price);

    @Insert("insert into borrow(sid, bid) values (#{sid},#{bid})")
    void borrowBook(@Param("sid") int sid, @Param("bid") int bid);

    @Delete("delete from borrow where id=#{id}")
    void returnBook(Integer id);

    @Results({
            @Result(column = "id", property = "id"),
            @Result(column = "bid", property = "bookId"),
            @Result(column = "title", property = "bookName"),
            @Result(column = "sid", property = "studentId"),
            @Result(column = "name", property = "studentName"),
            @Result(column = "time", property = "time")
    })
    @Select("select * from borrow,student,book where borrow.sid=student.sid and borrow.bid=book.bid")
    List<Borrow> borrowInfo(int id);

    @Update("update book set status = #{flag} where bid=#{bid}")
    void updateBook(@Param("bid") Integer bid, @Param("flag") boolean flag);

    @Select("SELECT COUNT(*) FROM book")
    Integer getBookQuantity();

    @Select("SELECT COUNT(*) FROM borrow")
    Integer getBorrowCount();
}
